﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Windows.Forms;

namespace VirtualDrive
{
    public partial class VirtualFolder : Form
    {
        public VirtualFolder()
        {
            InitializeComponent();
            CheckForIllegalCrossThreadCalls = false;
            this.Width += 1;
            _VirtualDirectory = "VB:\\";
        }
        public string[] VFiles { get; set; }

        public string _VirtualDirectory { get; set; }

        private void VirtualFolder_Load(object sender, EventArgs e)
        {

        }

        private void VirtualFolder_SizeChanged(object sender, EventArgs e)
        {
            panel2.Left = panel1.Left + (panel1.Width / 2) - pictureBox1.Width / 2;
            textBox1.Top = label1.Top = ClientRectangle.Top + 1;//done
            label1.Left = ClientRectangle.Left + 1;//done
            textBox1.Width = ClientRectangle.Width - (label1.Width - 2);//done
            textBox1.Left = label1.Right - 4; //done
            listBox1.Top = textBox1.Bottom;
            listBox1.Left = ClientRectangle.Left;
            listBox1.Width = ClientRectangle.Width;
            listBox1.Height = ClientRectangle.Height - (textBox1.Height) - panel1.Height;


        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {

         //   string RemoveY = "*?\"<>|";

            string temp = textBox1.Text;

           /* Task RemoveCh = new Task(() =>
            {
                foreach (Char removex in textBox1.Text.Where(x => RemoveY.Contains(x.ToString())))
                {

                    temp = temp.Replace(removex.ToString(), "");
                }

                textBox1.Text = temp.Substring(4, temp.Length - 4).Contains(":") ? "VB:\\" + temp.Substring(4, temp.Length - 4).Replace(":", "") : temp;


            }); RemoveCh.Start(); RemoveCh.Wait();            */


        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void panel2_DragEnter(object sender, DragEventArgs e)
        {
            e.Effect = DragDropEffects.Copy;
        }

        private void panel2_DragDrop(object sender, DragEventArgs e)
        {
            string[] Filesx = (string[])e.Data.GetData(DataFormats.FileDrop);
            try
            {
                //  _VBox._Add_INDEX(_VirtualDirectory,)
               _VBox._Add_INDEX(_VirtualDirectory,Filesx[0].Remove(Filesx[0].LastIndexOf('\\') + 1));
                foreach (string filec in Filesx)//error occurs when recycle bin is dropped
                {
                    List<string> Temp = new List<string>();
                    foreach (string item in listBox1.Items) { Temp.Add(item); }

                    if (CheckText._AddFile(Temp, filec.Substring(filec.LastIndexOf('\\') + 1)) != false)
                    {
                        if (File.Exists(filec))
                        {
                            pictureBox1.Image = Properties.Resources.loadingGif;
                            Task WaitForFile = new Task(() =>
                            {
                                try
                                 {
                                     _VBox._Add(_VirtualDirectory, filec.Substring(filec.LastIndexOf('\\') + 1));
                                 //    MessageBox.Show(filec.Substring(filec.LastIndexOf('\\') + 1));
                                 //   _VBox._Add_INDEX()
                                     string Output = Application.StartupPath + "\\Repository\\VB\\" + _VirtualDirectory.Substring(4) + filec.Substring(filec.LastIndexOf('\\') + 1);

                                     _VBox._TobyeFile(filec, Output);
                                    // _VBox._TobyeFile(filec,Output);
                                   
                                    
                                    //  _VBox._TobyeFile(filec,)
                                }
                                catch (UnauthorizedAccessException)
                                {
                             //       MessageBox.Show("You are unauthorized to access certain files. 'DragDrop' them again whilsting running the application in administrative mode.");
                                }
                            }); WaitForFile.Start();

                            WaitForFile.Wait();
                            if (WaitForFile.IsCompleted != false)
                            {
                                pictureBox1.Image = Properties.Resources._1399582265_arrow_up;
                            }
                        }
                        else if (Directory.Exists(filec))
                        {


                            pictureBox1.Image = Properties.Resources.loadingGif;


                            Task WaitFor = new Task(() =>
                            {
                                try
                                {


                                    string Temp_2 = filec.Substring(filec.LastIndexOf('\\') + 1);
                                    _VBox._Add(_VirtualDirectory, Temp_2.Substring(Temp_2.LastIndexOf('\\') + 1));


                                    string Output0 = Application.StartupPath + "\\Repository\\VB\\" + _VirtualDirectory.Substring(4) + Temp_2.Substring(Temp_2.LastIndexOf('\\') + 1);
                                    _VBox._TobyeFile(filec, Output0);

                                   
                                    foreach (var s_dir in Directory.GetDirectories(filec, ".", SearchOption.AllDirectories))
                                    {

                                        string xpath_segment = s_dir.Substring(s_dir.IndexOf(Temp_2));
                                        _VBox._Add(_VirtualDirectory + xpath_segment.Remove(xpath_segment.LastIndexOf("\\")) + "\\", xpath_segment.Substring(xpath_segment.LastIndexOf('\\') + 1));


                                        string Output1 = Application.StartupPath + "\\Repository\\VB\\" + _VirtualDirectory.Substring(4) + xpath_segment.Remove(xpath_segment.LastIndexOf("\\")) + "\\" + xpath_segment.Substring(xpath_segment.LastIndexOf('\\') + 1);
                                       _VBox._CreateDirectory( Output1); 
                                        
                             //       _VBox._Add_INDEX(_VirtualDirectory + xpath_segment.Remove(xpath_segment.LastIndexOf("\\")) + "\\", s_dir);
                                        //Transiever  [o]/[0]/[0] --> [0] -- [0] -- [0]
                                         
                                    }

                                    foreach (var s_dir in Directory.GetFiles(filec, ".", SearchOption.AllDirectories))
                                    {
                                       
                                        string xpath_segment = s_dir.Substring(s_dir.IndexOf(Temp_2));

                                        _VBox._Add(_VirtualDirectory + xpath_segment.Remove(xpath_segment.LastIndexOf("\\")) + "\\", xpath_segment.Substring(xpath_segment.LastIndexOf('\\') + 1));

                                        string Output1 = Application.StartupPath + "\\Repository\\VB\\" + _VirtualDirectory.Substring(4) + xpath_segment.Remove(xpath_segment.LastIndexOf("\\")) + "\\" + xpath_segment.Substring(xpath_segment.LastIndexOf('\\') + 1);
                                        _VBox._TobyeFile(s_dir, Output1);

                                       
                                        //  MessageBox.Show(xpath_segment + "-->" + xpath_segment.Substring(xpath_segment.LastIndexOf('\\') + 1));
                                        //Transiever  [o]/[0]/[0] --> [0] -- [0] -- [0]
                              //   _VBox._Add_INDEX(_VirtualDirectory + xpath_segment.Remove(xpath_segment.LastIndexOf("\\")) + "\\", s_dir);
                                     
                                    }
                                }
                                catch (UnauthorizedAccessException)
                                {
                            //        MessageBox.Show("You are unauthorized to access certain files. 'DragDrop' them again whilsting running the application in administrative mode.");
                                }

                            }); WaitFor.Start(); WaitFor.Wait();
                            if (WaitFor.IsCompleted != false)
                            {
                                pictureBox1.Image = Properties.Resources._1399582265_arrow_up;
                            }


                            //Hide Icon
                        }

                        //Compute the filec

                    }

                    Temp.Clear();


                }

                listBox1.Items.Clear();
                List<string> _getList = _VBox._getList(_VirtualDirectory);


                foreach (string vfile in _getList)
                {
                    listBox1.Items.Add(vfile);
                }






                //file.Substring(Files[0].LastIndexOf('\\') + 1)

                /*      switch (Files.Length)
                      {
                          case 1: //One File Dropped
                        //  if(   CheckText.CanAdd(listBox1.Items,)
                              break;

                          default: //More than One File Dropped
                              break;
                      }*/


            }
            catch (Exception) { }
        }
        
      
        private void button1_Click(object sender, EventArgs e)
        {
           // _VBox._Add(textBox1.Text,textBox2.Text);
           _VBox.ShowValues();
        
         
        }

        private void button2_Click(object sender, EventArgs e)
        {
            _VBox._Condense();
          /*  if (_VBox._getList(textBox1.Text) == null)
            {
                Console.WriteLine("Failed");
            }
            foreach (string message in _VBox._getList(textBox1.Text))
            {
                Console.WriteLine(message);
            }*/
       
        }

        private void listBox1_DoubleClick(object sender, EventArgs e)
        {
            //empty =0;
            if (CheckText.StringisEmpty(listBox1.SelectedItem) != true)
            {
              //  if (!listBox1.SelectedItem.ToString().Contains("."))
              
                     
                    List<string> _getList = _VBox._getList(_VirtualDirectory + listBox1.SelectedItem.ToString() + "\\");

                    if (_getList[0] != "null")
                    {
                        _VirtualDirectory = _VirtualDirectory + listBox1.SelectedItem.ToString() + "\\";
                        textBox1.Text = _VirtualDirectory.Replace("VB:\\", "");
                        listBox1.Items.Clear();

                        foreach (string vfile in _getList)
                        {
                            listBox1.Items.Add(vfile);
                        }
                    }


            }
        }

        private void textBox1_KeyUp(object sender, KeyEventArgs e)
        {

            if (e.KeyValue == 13)
            {


                _VirtualDirectory = "VB:\\" + textBox1.Text;
                listBox1.Items.Clear();
                List<string> _getList = _VBox._getList(_VirtualDirectory);


                foreach (string vfile in _getList)
                {
                    listBox1.Items.Add(vfile);
                }
              /*  try
                {
                    if (!string.IsNullOrEmpty(_getList[0]))
                    {

                    }
                }
                catch (Exception) { }*/
            }
            else
            {

            }
        }
    }
}